home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / forth / amiga / amigaker.arc / 14.compiler < prev    next >
Encoding:
Text File  |  1987-12-30  |  19.5 KB  |  516 lines

  1. ;
  2. ;  14.compiler
  3. ;
  4. ;
  5.  
  6.  
  7. * topstack        (s -- n ) This constant is initialized at the start, and
  8. ; is used to do stack checking.
  9.                   dc.w     -1
  10.                   dc.l     link0
  11. link0             set      *-4
  12.                   dc.b     $88,'topstac',$80!'k'
  13.                   cnop     0,2
  14. _topstack         dc.l     doconstant
  15. topstack          dc.l     0
  16.  
  17. * ?stack          (s -- ) If the stack under or overflows issue a
  18. ; message and abort.
  19.                   dc.w     -1
  20.                   dc.l     link3
  21. link3             set      *-4
  22.                   dc.b     $86,'?stac',$80!'k'
  23.                   cnop     0,2
  24. _question_stack   dc.l     nest
  25.                   dc.l     _sp_fetch,_sp0,_fetch,_swap
  26.                   dc.l     _u_less,_nest_abort_quote
  27.                   dc.b     16,'Stack Underflow',0
  28.                   cnop     0,2
  29.                   dc.l     _sp_fetch,_topstack,_u_less
  30.                   dc.l     _nest_abort_quote
  31.                   dc.b     15,'Stack Overflow',0
  32.                   cnop     0,2
  33.                   dc.l     _exit
  34.  
  35. * status          A deferred word usually set to cr. This word is used in
  36. ; the 'quit' loop, when interpreting and can be used to print a status line,
  37. ; print a prompt etc.
  38.                   dc.w     -1
  39.                   dc.l     link3
  40. link3             set      *-4
  41.                   dc.b     $86,'statu',$80!'s'
  42.                   cnop     0,2
  43. _status           dc.l     dodefer,_cr
  44.  
  45. * interpret       (s -- ) The interpret loop, if the next word is defined
  46. ; execute it, otherwise convert it to a number and push it onto the stack.
  47. ; The loop will end when the end of input is found or when the state
  48. ; changes.
  49.                   dc.w     -1
  50.                   dc.l     link1
  51. link1             set      *-4
  52.                   dc.b     $89,'interpre',$80!'t'
  53.                   cnop     0,2
  54. _interpret        dc.l     nest
  55. 1$                dc.l     _question_stack,_defined
  56.                   dc.l     _question_branch,2$
  57.                   dc.l       _execute,_branch,3$
  58. 2$                dc.l     _number,_double_question,_not
  59.                   dc.l     _question_branch,3$
  60.                   dc.l     _drop
  61. 3$                dc.l     _false,_done_question
  62.                   dc.l     _question_branch,1$
  63.                   dc.l     _exit
  64.  
  65. * allot           (s n -- ) Allocate n bytes in the dictionary.
  66. ;   NOTE: be very carefull, allocate only even bytes, or else the guru
  67. ;         will appear.
  68.                   dc.w     -1
  69.                   dc.l     link1
  70. link1             set      *-4
  71.                   dc.b     $85,'allo',$80!'t'
  72.                   cnop     0,2
  73. _allot            dc.l     nest
  74.                   dc.l     _dp,_plus_store,_exit
  75.  
  76. * ,               (s n -- ) Store top of the stack in the dictionary.
  77. ; Since this is a 32 bit sytem, 4 bytes are used.
  78.                   dc.w     -1
  79.                   dc.l     link0
  80. link0             set      *-4
  81.                   dc.b     $81,$80!$2c
  82.                   cnop     0,2
  83. _comma            dc.l     nest
  84.                   dc.l     _here,_store,_4,_allot,_exit
  85.  
  86. * w,              (s w -- ) Stores lower 16bits of tos in the dictionary
  87.                   dc.w     -1
  88.                   dc.l     link3
  89. link3             set      *-4
  90.                   dc.b     $82,'w',$80!$2c
  91.                   cnop     0,2
  92. _w_comma          dc.l     nest
  93.                   dc.l     _here,_w_store,_2,_allot,_exit
  94.  
  95. * c,              (s c -- ) Store the character from the tos in the
  96. ; dictionary.   WARNING must align the dictionary after c,
  97.                   dc.w     -1
  98.                   dc.l     link3
  99. link3             set      *-4
  100.                   dc.b     $82,'c',$80!$2c
  101.                   cnop     0,2
  102. _c_comma          dc.l     nest
  103.                   dc.l     _here,_c_store,_1,_allot,_exit
  104.  
  105. * align           (s -- ) word align the dictionary.
  106.                   dc.w     0
  107.                   dc.l     link1
  108. link1             set      *-4
  109.                   dc.b     $85,'alig',$80!'n'
  110.                   cnop     0,2
  111. _align            dc.l     nest
  112.                   dc.l     _here,_1,_and,_question_branch,1$
  113.                   dc.l     _0,_c_comma
  114. 1$                dc.l     _exit
  115.  
  116. * even            (s addr -- addr' ) Force the address on the stack
  117. ; to be even.
  118.                   dc.w     -1
  119.                   dc.l     link1
  120. link1             set      *-4
  121.                   dc.b     $84,'eve',$80!'n'
  122.                   cnop     0,2
  123. _even             dc.l     nest
  124.                   dc.l     _dup,_1,_and,_plus,_exit
  125.  
  126. * compile         (s -- ) Compile the following word when this def.
  127. ; executes.
  128.                   dc.w     -1
  129.                   dc.l     link3
  130. link3             set      *-4
  131.                   dc.b     $87,'compil',$80!'e'
  132.                   cnop     0,2
  133. _compile          dc.l     nest
  134.                   dc.l     _r_from,_dup,_4_plus,_to_r
  135.                   dc.l     _fetch,_comma,_exit
  136.  
  137. * immediate       (s -- ) Mark the last header as immediate.
  138.                   dc.w     -1
  139.                   dc.l     link1
  140. link1             set      *-4
  141.                   dc.b     $89,'immediat',$80!'e'
  142.                   cnop     0,2
  143. _immediate        dc.l     nest
  144.                   dc.l     _nest_lit,immediate
  145.                   dc.l     _last,_fetch,_cset,_exit
  146.  
  147. * literal         (s n -- ) compile tos as a literal.
  148.                   dc.w     -1
  149.                   dc.l     link0
  150. link0             set      *-4
  151.                   dc.b     $87!immediate,'litera',$80!'l'
  152.                   cnop     0,2
  153. _literal          dc.l     nest
  154.                   dc.l     _compile,_nest_lit,_comma,_exit
  155.  
  156. * dliteral        (s d -- ) compile the double on the stack as a double lit.
  157.                   dc.w     -1
  158.                   dc.l     link0
  159. link0             set      *-4
  160.                   dc.b     $88!immediate,'dlitera',$80!'l'
  161.                   cnop     0,2
  162. _dliteral         dc.l     nest
  163.                   dc.l     _swap,_literal,_literal,_exit
  164.  
  165. * ascii           (s -- n ) A state smart word. Returns ascii value of the
  166. ; following character. If compiling will compile the literal value of the
  167. ; character.
  168.                   dc.w     -1
  169.                   dc.l     link1
  170. link1             set      *-4
  171.                   dc.b     $85!immediate,'asci',$80!'i'
  172.                   cnop     0,2
  173. _ascii            dc.l     nest
  174.                   dc.l     _bl,_word,_1_plus,_c_fetch
  175.                   dc.l     _state,_fetch,_question_branch,1$
  176.                   dc.l     _literal
  177. 1$                dc.l     _exit
  178.  
  179. * control         (s -- n ) Same as ascii, but for control characters.
  180.                   dc.w     -1
  181.                   dc.l     link3
  182. link3             set      *-4
  183.                   dc.b     $87!immediate,'contro',$80!'l'
  184.                   cnop     0,2
  185. _control          dc.l     nest
  186.                   dc.l     _bl,_word,_1_plus,_c_fetch,_nest_lit,31
  187.                   dc.l     _and,_state,_fetch,_question_branch,1$
  188.                   dc.l     _literal
  189. 1$                dc.l     _exit
  190.  
  191. * crash           (s -- ) This routine is the default for deferred words.
  192.                   dc.w     -1
  193.                   dc.l     link3
  194. link3             set      *-4
  195.                   dc.b     $85,'cras',$80!'h'
  196.                   cnop     0,2
  197. _crash            dc.l     nest
  198.                   dc.l     _true,_nest_abort_quote
  199.                   dc.b     33,' Uninitialized execution vector.',0
  200.                   cnop     0,2
  201.                   dc.l     _exit
  202.  
  203. * ?missing        (s f -- ) This word is executed when the word is not found
  204. ; and if it is not convertable to a number. See number.
  205.                   dc.w     -1
  206.                   dc.l     link3
  207. link3             set      *-4
  208.                   dc.b     $88,'?missin',$80!'g'
  209.                   cnop     0,2
  210. _question_missing dc.l     nest
  211.                   dc.l     _question_branch,1$
  212.                   dc.l     _tick_word,_count,_type,_true
  213.                   dc.l     _nest_abort_quote
  214.                   dc.b     3,' ?',0
  215.                   cnop     0,2
  216. 1$                dc.l     _exit
  217.  
  218. * '               (s -- cfa ) returns code field of the following word.
  219.                   dc.w     -1
  220.                   dc.l     link3
  221. link3             set      *-4
  222.                   dc.b     $81,$80!$27
  223.                   cnop     0,2
  224. _tick             dc.l     nest
  225.                   dc.l     _defined,_0_equal,_question_missing
  226.                   dc.l     _exit
  227.  
  228. * [']             (s -- ) Compiles the cfa of the next word. Used when
  229. ; compiling.
  230.                   dc.w     -1
  231.                   dc.l     link3
  232. link3             set      *-4
  233.                   dc.b     $83!immediate,$5b,$27,$80!$5d
  234.                   cnop     0,2
  235. _bracket_tick     dc.l     nest
  236.                   dc.l     _tick,_literal,_exit
  237.  
  238. * [compile]       (s -- ) Force compilation of an immediate word.
  239.                   dc.w     -1
  240.                   dc.l     link3
  241. link3             set      *-4
  242.                   dc.b     $89!immediate,$5B,'compile',$80!$5d
  243.                   cnop     0,2
  244. _bracket_compile  dc.l     nest
  245.                   dc.l     _tick,_comma,_exit
  246.  
  247. * (")             (s -- addr len ) returns address and length of the inline
  248. ; string.
  249.                   dc.w     -1
  250.                   dc.l     link0
  251. link0             set      *-4
  252.                   dc.b     $83,$28,$22,$80!$29
  253.                   cnop     0,2
  254. _nest_quote       dc.l     nest
  255.                   dc.l     _r_from,_count,_2dup,_plus
  256.                   dc.l     _even,_to_r,_exit
  257.  
  258. * (.")            (s -- ) Types the inline string.
  259.                   dc.w     -1
  260.                   dc.l     link2
  261. link2             set      *-4
  262.                   dc.b     $84,$28,$2E,$22,$80!$29
  263.                   cnop     0,2
  264. _nest_dot_quote   dc.l     nest
  265.                   dc.l     _r_from,_count,_2dup,_plus
  266.                   dc.l     _even,_to_r,_type,_exit
  267.  
  268. * ,"              (s -- ) Adds the text upto the next double quote to the
  269. ; dictionary. The text has a null appended, to make it compatible with
  270. ; the Amiga.
  271.                   dc.w     -1
  272.                   dc.l     link0
  273. link0             set      *-4
  274.                   dc.b     $82,$2c,$80!$22
  275.                   cnop     0,2
  276. _comma_quote      dc.l     nest
  277.                   dc.l     _nest_lit,'"',_parse,_1_plus,_tuck
  278.                   dc.l     _tick_word,_place
  279.                   dc.l     _allot,_0,_c_comma,_align,_exit
  280.  
  281. * ."              (s -- ) Compile the string to be typed out when word
  282. ; executes.
  283.                   dc.w     -1
  284.                   dc.l     link2
  285. link2             set      *-4
  286.                   dc.b     $82!immediate,$2E,$80!$22
  287.                   cnop     0,2
  288. _dot_quote        dc.l     nest
  289.                   dc.l     _compile,_nest_dot_quote
  290.                   dc.l     _comma_quote,_exit
  291.  
  292. * "               (s -- ) Compile the string, returns a pointer later.
  293.                   dc.w     -1
  294.                   dc.l     link2
  295. link2             set      *-4
  296.                   dc.b     $81!immediate,$80!$22
  297.                   cnop     0,2
  298. _quote            dc.l     nest
  299.                   dc.l     _compile,_nest_quote
  300.                   dc.l     _comma_quote,_exit
  301.  
  302. * fenced          (S addr -- fl )  True if addr is in userdictionary.
  303. ; This word is special, it knows where and how long the userdictionary is.
  304. ; This routine is necessary to be able to 'forget' words. The kernel is not
  305. ; forgettable and is not sequentially inline with the user dictionary.
  306.                   dc.w     -1
  307.                   dc.l     link2
  308. link2             set      *-4
  309.                   dc.b     $86,'fence',$80!'d'
  310.                   cnop     0,2
  311. _fenced           dc.l     *+4
  312.                   move.l   (sp),d0
  313.                   lea      _dp+8,a0       ;point to area after dp
  314.                   sub.l    (a0)+,d0       ;the user dictionary start and
  315.                   bmi      no             ; size are there.
  316.                   sub.l    (a0),d0
  317.                   bmi      yes
  318.                   bra      no
  319.  
  320. * close-lib       (s lib-link -- ) Close the library at the cell before
  321. ; this library link.
  322.                   dc.w     -1
  323.                   dc.l     link3
  324. link3             set      *-4
  325.                   dc.b     $89,'close-li',$80!'b'
  326.                   cnop     0,2
  327. _close_lib        dc.l     nest
  328.                   dc.l     _4_minus,_dup,_fetch,_question_dup
  329.                   dc.l     _question_branch,1$
  330.                   dc.l     _CloseLibrary,_off,_branch,2$
  331. 1$                dc.l     _drop
  332. 2$                dc.l     _exit
  333.  
  334. * trim            (s faddr voc-addr -- ) Adjusts the 4 linked lists in a
  335. ; vocabulary, so they are all less then a specified value, faddr.
  336.                   dc.w     -1
  337.                   dc.l     link0
  338. link0             set      *-4
  339.                   dc.b     $84,'tri',$80!'m'
  340.                   cnop     0,2
  341. _trim             dc.l     nest
  342.                   dc.l     _number_threads,_0,_nest_do,4$
  343. 1$                dc.l     _2dup,_fetch
  344. 2$                dc.l     _2dup,_dup,_fenced,_minus_rot
  345.                   dc.l     _u_greater,_not,_and
  346.                   dc.l     _question_branch,3$
  347.                   dc.l     _fetch,_branch,2$
  348. 3$                dc.l     _nip,_over,_store,_4_plus
  349.                   dc.l     _nest_loop,1$
  350. 4$                dc.l     _2drop,_exit
  351.  
  352. * tonext          (s faddr linkpointer -- faddr linkpointer flag )
  353. ; Used with forgetting.
  354. ; Returns a true flag if the linkpointer is within the fenced area and larger
  355. ; than the faddr.
  356.                   dc.w     -1
  357.                   dc.l     link0
  358. link0             set      *-4
  359.                   dc.b     $86,'tonex',$80!'t'
  360.                   cnop     0,2
  361. _tonext           dc.l     nest
  362.                   dc.l     _2dup,_dup,_fenced,_minus_rot
  363.                   dc.l     _u_less,_and,_exit
  364.  
  365. * (forget)        (s view-addr -- ) Forgets part of the dictionary.
  366. ; Adjusts library links, closing them if necessary, closes files and adjusts
  367. ; the file linked list before the words referring to files are forgotten
  368. ; and open files are left, requiring a reset.
  369. ; If, for some reason, seperate headers are used, the word changes, but
  370. ; functionally performs identically. It must release the headers and the
  371. ; code.
  372.                   dc.w     -1
  373.                   dc.l     link0
  374. link0             set      *-4
  375.                   dc.b     $88,'(forget',$80!')'
  376.                   cnop     0,2
  377. _nest_forget      dc.l     nest
  378.                   dc.l     _dup,_fenced,_not
  379.                   dc.l     _nest_abort_quote
  380.                   dc.b     12,'Below fence',0
  381.                   cnop     0,2
  382.                   dc.l     _lib_link,_fetch
  383. 1$                dc.l     _tonext,_question_branch,2$
  384.                   dc.l     _dup,_close_lib,_fetch,_branch,1$
  385. 2$                dc.l     _lib_link,_store
  386.                   ;library links resolved. (s faddr -- )
  387.                   dc.l     _file_link,_fetch
  388. 3$                dc.l     _tonext,_question_branch,4$
  389.                   dc.l     _dup,_close_file,_fetch,_branch,3$
  390. 4$                dc.l     _file_link,_store
  391.                   ;file links resolved     (s faddr -- )
  392.                   dc.l     _voc_link,_fetch
  393. 5$                dc.l     _tonext,_question_branch,6$
  394.                   dc.l     _fetch,_branch,5$
  395. 6$                dc.l     _dup,_voc_link,_store
  396. 7$                dc.l     _dup,_question_branch,8$
  397.                   dc.l     _2dup,_number_threads,_4_times
  398.                   dc.l     _minus,_trim,_fetch,_branch,7$
  399. 8$                dc.l     _drop,_dp,_store,_exit
  400.  
  401. * forget          (s -- ) <word>
  402. ; Forgets all headers and code before <word>.
  403.                   dc.w     -1
  404.                   dc.l     link2
  405. link2             set      *-4
  406.                   dc.b     $86,'forge',$80!'t'
  407.                   cnop     0,2
  408. _forget           dc.l     nest
  409.                   dc.l     _bl,_word,_question_uppercase,_dup
  410.                   dc.l     _current,_fetch,_hash,_fetch,_nest_find
  411.                   dc.l     _0_equal,_question_missing,_to_view
  412.                   dc.l     _nest_forget,_exit
  413.  
  414. * close-libs      (s -- ) Close all libraries by traversing the linked list
  415. ; and close any library which is open. Called by bye.
  416.                   dc.w     -1
  417.                   dc.l     link3
  418. link3             set      *-4
  419.                   dc.b     $8A,'close-lib',$80!'s'
  420.                   cnop     0,2
  421. _close_libs       dc.l     nest
  422.                   dc.l     _lib_link
  423. 1$                dc.l     _fetch,_question_dup,_question_branch,2$
  424.                   dc.l     _dup,_close_lib
  425.                   dc.l     _branch,1$
  426. 2$                dc.l     _exit
  427.  
  428. * where           A deferred word, currently (where), prints all the
  429. ; loadbuffers, unnesting the loading process.
  430.                   dc.w     -1
  431.                   dc.l     link3
  432. link3             set      *-4
  433.                   dc.b     $85,'wher',$80!'e'
  434.                   cnop     0,2
  435. _where            dc.l     dodefer,_nest_where
  436.  
  437. * (where)         (s index -- ) Print out the loadbuffer, index is where
  438. ; the loading problem occured. Print all the buffers nested.
  439.                   dc.w     -1
  440.                   dc.l     link0
  441. link0             set      *-4
  442.                   dc.b     $87,'(where',$80!')'
  443.                   cnop     0,2
  444. _nest_where       dc.l     nest
  445.                   dc.l     _lb,_fetch,_4_plus,_count,_cr,_type,_cr
  446.                   dc.l     _spaces,_nest_lit,$5E,_emit,_cr
  447. 1$                dc.l     _droplb,_lb,_fetch,_question_dup
  448.                   dc.l     _question_branch,2$
  449.                   dc.l     _4_plus,_count,_type,_cr,_branch,1$
  450. 2$                dc.l     _exit
  451.  
  452. * ?error          Deferred word, currently (?error). Abort ends up here
  453. ; change this to alter aborts behaviour.
  454.                   dc.w     -1
  455.                   dc.l     link3
  456. link3             set      *-4
  457.                   dc.b     $86,'?erro',$80!'r'
  458.                   cnop     0,2
  459. _question_error   dc.l     dodefer,_nest_question_error
  460.  
  461. * (?error)        (s addr len f -- ) Conditionally type the string and if
  462. ; input is from the disk, execute where.
  463.                   dc.w     -1
  464.                   dc.l     link0
  465. link0             set      *-4
  466.                   dc.b     $88,'(?error',$80!')'
  467.                   cnop     0,2
  468. _nest_question_error
  469.                   dc.l     nest
  470.                   dc.l     _question_branch,2$
  471.                   dc.l     _to_r,_to_r,_sp0,_fetch,_sp_store
  472.                   dc.l     _loading,_fetch,_question_branch,1$
  473.                   dc.l     _to_in,_fetch,_where
  474. 1$                dc.l     _r_from,_r_from,_space,_type,_space
  475.                   dc.l     _quit,_branch,3$
  476. 2$                dc.l     _2drop
  477. 3$                dc.l     _exit
  478.  
  479. * (abort")        (s f -- ) Runtime for abort", calls ?error and
  480. ; adjusts the stack
  481.                   dc.w     -1
  482.                   dc.l     link0
  483. link0             set      *-4
  484.                   dc.b     $88,'(abort"',$80!')'
  485.                   cnop     0,2
  486. _nest_abort_quote
  487.                   dc.l     nest
  488.                   dc.l     _r_fetch,_count,_rot,_question_error
  489.                   dc.l     _r_from,_count,_plus,_even,_to_r
  490.                   dc.l     _exit
  491.  
  492. * abort"          (s -- ) Compiles a string to be typed if the flag is
  493. ; true and then quits by running quit.
  494.                   dc.w     -1
  495.                   dc.l     link1
  496. link1             set      *-4
  497.                   dc.b     $86!immediate,'abort',$80!'"'
  498.                   cnop     0,2
  499. _abort_quote      dc.l     nest
  500.                   dc.l     _compile,_nest_abort_quote
  501.                   dc.l     _comma_quote,_exit
  502.  
  503. * abort           (s -- ) Stop the system.
  504.                   dc.w     -1
  505.                   dc.l     link1
  506. link1             set      *-4
  507.                   dc.b     $85,'abor',$80!'t'
  508.                   cnop     0,2
  509. _abort            dc.l     nest
  510.                   dc.l     _true,_nest_abort_quote
  511.                   dc.b     1,0
  512.                   cnop     0,2
  513.                   dc.l     _exit
  514.  
  515.  
  516.